package com.daatak.application.business;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.daatak.application.dao.ApplicationDaoI;
import com.daatak.application.domain.ApplicationFinanceObject;
import com.daatak.application.domain.ApplicationObject;
import com.daatak.application.domain.ApplicationView;
import com.daatak.application.domain.AssetObject;
import com.daatak.customer.domain.CustomerAddress;
import com.daatak.customer.domain.CustomerObject;
import com.daatak.customer.domain.EmplomentObject;
import com.daatak.mvc.application.ApplicationPojo;

@Component
@Transactional
public class ApplicationBean implements ApplicationBeanI {
	
	@Autowired
	ApplicationDaoI applicationDao;
	
	public List<ApplicationView> list()
	{
		return applicationDao.list();
	}
	
	public void create(ApplicationPojo applicationPojo)
	{ 
		ApplicationObject ao = applicationPojo.getApplication();
		
		applicationPojo.getCustomerAddress().setReferenceType("APPLICANT");
		applicationPojo.getCustomerAddress().setAddressType("PHY_ADD");//temporary will be set automatically when the form field is introduced.

		//applicationPojo.getCustomerMortgage().setRefId(applicationPojo.getCustomer().getCustomerId());
		applicationPojo.getCustomer().getEmployment().add(applicationPojo.getCustomerEmployment());
		//applicationPojo.getCustomer().setMortgage(applicationPojo.getCustomerMortgage());
		//applicationPojo.getCustomerMortgage().setCustomer(applicationPojo.getCustomer());
		applicationPojo.getCustomer().getAddress().add(applicationPojo.getCustomerAddress());
		
		if(applicationPojo.getApplication().getNoOfApplicant()>1){
			applicationPojo.getCoBorrowerAddress().setReferenceType("CO-APPLICANT");
			applicationPojo.getCoBorrowerAddress().setAddressType("PHY_ADD");
			applicationPojo.getCoBorrower().getEmployment().add(applicationPojo.getCoBorrowerEmployment());
			applicationPojo.getCoBorrower().getAddress().add(applicationPojo.getCoBorrowerAddress());
		}
		
		ao.getCustomer().add(applicationPojo.getCustomer());
		applicationDao.create(ao);
		
	}
	
	public ApplicationObject findById(Integer Id)
	{
		return applicationDao.findById(Id);
	}
	public void delete(ApplicationPojo  applicationPojo)
	{
		applicationDao.delete(applicationPojo.getApplication());
	}


	public void setApplicationDao(ApplicationDaoI applicationDao) {
		this.applicationDao = applicationDao;
	}
	
	public void addAsset(ApplicationPojo applicationPojo)
	{ 
		ApplicationObject ao = applicationPojo.getApplication();
		
		applicationPojo.getAsset().setReferenceId(ao.getApplicationId());
		applicationPojo.getAsset().setReferenceType("APP");
		
		applicationDao.addAsset(applicationPojo.getAsset());
		
	}
	
	public List<AssetObject> listAssetByAppId(Integer applicationId) {
		
		return applicationDao.listAssetByAppId(applicationId);
	}
	public void calculateAndSave(ApplicationPojo applicationPojo)
	{
		
		ApplicationFinanceObject financeObject=applicationPojo.getFinance();
		financeObject.setApplicationId(applicationPojo.getApplication().getApplicationId());
		//applicationDao.save(financeObject);
		
		//TODO create a transaction manager to save the all object of application in a transaction;
	}


	
	public void loadApplication(ApplicationPojo applicationPojo,
			Integer applicationId) {
		// TODO: correct the population of the objects like customer, co-borrower and their addresses that should not be like that. the logic written below is riddiculas
		ApplicationObject app= applicationDao.loadApplicationById(applicationId);
		applicationPojo.setApplication(app);
		
		for (Iterator iterator = app.getCustomer().iterator(); iterator.hasNext();) {
			CustomerObject customer = (CustomerObject) iterator.next();
			if("APPLICATION".equals(customer.getReferenceType())){
				applicationPojo.setCustomer(customer);
				applicationPojo.setCustomerMortgage(customer.getMortgage());
			}
			else
				applicationPojo.setCoBorrower(customer);
			
			
		}
		for (Iterator iterator = applicationPojo.getCustomer().getAddress().iterator(); iterator.hasNext();) {
			CustomerAddress address = (CustomerAddress) iterator.next();
			if("PHY_ADD".equals(address.getAddressType())){
				applicationPojo.setCustomerAddress(address);
			}
			
		}
		for (Iterator iterator = applicationPojo.getCustomer().getEmployment().iterator(); iterator.hasNext();) {
			EmplomentObject employement = (EmplomentObject) iterator.next();
			applicationPojo.setCustomerEmployment(employement);
			
		}
		if(applicationPojo.getCoBorrower()!=null){
			for (Iterator iterator = applicationPojo.getCoBorrower().getAddress().iterator(); iterator.hasNext();) {
				CustomerAddress address = (CustomerAddress) iterator.next();
				if("PHY_ADD".equals(address.getAddressType())){
					applicationPojo.setCoBorrowerAddress(address);
				}
				
			}
			for (Iterator iterator = applicationPojo.getCoBorrower().getEmployment().iterator(); iterator.hasNext();) {
				EmplomentObject employement = (EmplomentObject) iterator.next();
				applicationPojo.setCoBorrowerEmployment(employement);
				
			}
		}
	}

	public ApplicationFinanceObject loadApplicationFinancial(Integer applicationId)
	{
		
		return applicationDao.loadApplicationFinancial(applicationId);
	}
}
